package com.user.controller;

import com.common.domain.dto.AppointmentDto;
import com.common.domain.entity.ResponseResult;
import com.common.domain.vo.VisitRecordVo;
import com.common.service.ConsultationService;
import com.common.service.DoctorService;
import com.common.service.VisitRecordService;
import com.common.util.FileUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.text.ParseException;



@Slf4j
@RestController
public class AppointmentController {
    @Autowired
    private ConsultationService consultationService;
    @Autowired
    private DoctorService doctorService;
    @Autowired
    private VisitRecordService visitRecordService;
    private static String ROOTDIR= "/u01/Server/imgbase" +File.separator;

    @GetMapping("getDoctorInfo")
    public ResponseResult getDoctorInfo(){
        return doctorService.getDoctorInfo();
    }

    @GetMapping("getDoctorDetail")
    public ResponseResult getDoctorDetail(Long id){
        return doctorService.getDoctorDetail(id);
    }

    /**
     * 预约功能
     */
    @GetMapping("getDateAppointmentInfo")
    public ResponseResult getDateAppointmentInfo(Long id, String date) throws ParseException {

        return consultationService.getDateAppointmentInfo(id, date);
    }

    @GetMapping("/getDoctorLeaveInfo")
    public ResponseResult getDoctorLeaveInfo(Long id)  {
        return ResponseResult.okResult( doctorService.getDoctorLeaveInfo(id, "1"));
    }

    @RequestMapping(value = "/download", method = RequestMethod.GET)
    public void  download(HttpServletResponse response, String fileSavePath){
        fileSavePath = fileSavePath.replace("＼", File.separator);
        String dirName= ROOTDIR;
        String filePath = dirName + fileSavePath;
        try{
            File file = new File(filePath);
            if(! file.exists()) {
            }
        }catch (Exception e){

        }
        FileUtil.downLoad(response,filePath);
    }
    @RequestMapping(value = "/downloadPdf", method = RequestMethod.GET)
    public ResponseResult  downloadPdf(HttpServletResponse response, Long id) throws IOException {

        //通过id得到信息
        VisitRecordVo visitRecordVo = visitRecordService.getUserVisitRecode(id);
        String fileName = "template.pdf";
        InputStream inputStreamPdf = AppointmentController.class.getClassLoader().getResourceAsStream("downfilesource/"+fileName);

        return visitRecordService.downloadPdf(inputStreamPdf,visitRecordVo,response);
    }
    @GetMapping("/getVisitRecode")
    public ResponseResult getVisitRecode(Long id){
        return visitRecordService.getVisitRecode(id);
    }

    @PostMapping("/submitAppointmentInfo")
    public ResponseResult submitAppointmentInfo(@RequestBody AppointmentDto appointmentDto)  {


        return consultationService.submitAppointmentInfo(appointmentDto);
    }


    @PostMapping("/updateAppointmentInfo")
    public ResponseResult updateAppointmentInfo(@RequestBody AppointmentDto appointmentDto)  {


        return consultationService.updateAppointmentInfo(appointmentDto);
    }
    @GetMapping("/getAppointmentLog")
    public ResponseResult getAppointmentLog(String type, int pageNum, int pageSize)  {
        return ResponseResult.okResult(consultationService.getAppointmentLog(type, pageNum, pageSize));
    }

    @GetMapping("/cancelAppointmentLog")
    public ResponseResult cancelAppointmentLog(Long id)  {
        return consultationService.cancelAppointmentLog(id);
    }
}
